package org.kjtc.balanceController;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.kjtc.balanceEntity.BalanceBill;
import org.kjtc.balanceEntity.BalanceInitSetting;
import org.kjtc.balanceEntity.ChargingCostRatio;
import org.kjtc.balanceEntity.ChargingFixedPrice;
import org.kjtc.balanceEntity.ChargingOilPrice;
import org.kjtc.balanceEntity.ChargingTimePrice;
import org.kjtc.balanceService.CostManageService;
import org.kjtc.balanceService.PowerFactorService;
import org.kjtc.controller.BaseController;
import org.kjtc.security.MySecurityMetadataSource;
import org.kjtc.util.Const;
import org.kjtc.util.FileDownload;
import org.kjtc.util.FileUpload;
import org.kjtc.util.Tools;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

@Controller
public class CostManageController extends BaseController {

	@Resource(name = "costManageService")
	private CostManageService costManageService;
	
	@Resource(name = "powerFactorService")
	private PowerFactorService powerFactorService;

	@Resource(name = "mySecurityMetadataSource")
	private MySecurityMetadataSource mySecurityMetadataSource;

	@RequestMapping(value = "/searchCostBalanceBill")
	@ResponseBody
	public Map<String, Object> searchCostBalanceBill(BalanceBill balanceBill){

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "电力结算单查询");
		List<BalanceBill> list = null;
		try {
			list = costManageService.getBalanceBill(balanceBill);
			this.setSessionAttribute("costBalanceBillList", list);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "电力结算单查询异常" + e.getMessage());
			return map;
		}
		map.put("costBalanceBillList", list);
		balanceBill = null;
		return map;
	}

	@RequestMapping(value = "/balanceBillName")
	@ResponseBody
	public List<BalanceBill> getBalanceBillName(BalanceBill balanceBill){

		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "下拉列表-电力结算单查询");
		List<BalanceBill> list = null;
		try {
			list = costManageService.getBalanceBillName(balanceBill);
		} catch (Exception e) {
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "下拉列表-电力结算单查询异常:" + e.getMessage());
			return null;
		}
        return list;
	}

	@RequestMapping(value = "/costRatioName")
	@ResponseBody
	public List<ChargingCostRatio> getChargingCostRatioName(ChargingCostRatio chargingCostRatio){

		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "下拉列表-系数类型查询");
		List<ChargingCostRatio> list = null;
		try {
			list = costManageService.getChargingCostRatioName(chargingCostRatio);
		} catch (Exception e) {
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "下拉列表-系数类型查询异常:" + e.getMessage());
			return null;
		}
		return list;
	}

	@RequestMapping(value = "/searchCostRatio")
	@ResponseBody
	public Map<String, Object> searchCostRatio(ChargingCostRatio costRatio){

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "电费系数生成");
		this.setSessionAttribute("costRatioList", null);

		List<ChargingCostRatio> list = null;
		try {
			list = costManageService.getChargingCostRatio(costRatio);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "电费系数生成异常" + e.getMessage());
			return map;
		}

		map.put("costRatioList", list);

		costRatio = null;
		return map;
	}

	@RequestMapping(value = "/createCostRatio")
	@ResponseBody
	public Map<String, Object> createCostRatio(ChargingCostRatio costRatio){

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "电费系数创建");

		List<ChargingCostRatio> list = null;
		try {
			list = costManageService.createChargingCostRatio(costRatio);
			this.setSessionAttribute("costRatioList", list);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "电费系数创建异常" + e.getMessage());
			return map;
		}

		map.put("costRatioList", list);

		costRatio = null;
		return map;
	}

	@SuppressWarnings("unchecked")
	@ResponseBody
	@RequestMapping(value = "/saveCostRatio")
	public Map<String, Object> saveCostRatio(String BalanceBillID, String balanceMonth, String fixedRatio){

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "电费系数添加");

		try {
			List<ChargingCostRatio> costRatioList = (List<ChargingCostRatio>)this.getSessionAttribute("costRatioList");
			if (costRatioList.size() > 0) {
				double tradeKWHTotal = 0;
				double chargingKWHTotal = 0;
				int chargingRecordCount = 0;
				for (ChargingCostRatio chargingCostRatio : costRatioList) {
					tradeKWHTotal = tradeKWHTotal + chargingCostRatio.getTradeKWH();
					chargingKWHTotal = chargingKWHTotal + chargingCostRatio.getChargingKWH();
					chargingRecordCount = chargingRecordCount + chargingCostRatio.getChargingRecordCount();
					chargingCostRatio.setBalanceMonth(balanceMonth);
					chargingCostRatio.setBalanceBillID(BalanceBillID);
					chargingCostRatio.setBalanceRatioType("3");
					chargingCostRatio.setCreateUser(this.getUesr().getUserID());
					chargingCostRatio.setUpdateUser(this.getUesr().getUserID());
				}
				ChargingCostRatio CostTotalRatio = new ChargingCostRatio();
				CostTotalRatio.setBalanceMonth(balanceMonth);
				CostTotalRatio.setBalanceBillID(BalanceBillID);
				CostTotalRatio.setBalanceRatioType("2");
				CostTotalRatio.setChargingStationID("2");
				if (chargingKWHTotal > 0) {
					CostTotalRatio.setBalanceRatio(tradeKWHTotal/chargingKWHTotal);
				}
				CostTotalRatio.setTradeKWH(tradeKWHTotal);
				CostTotalRatio.setChargingKWH(chargingKWHTotal);
				CostTotalRatio.setChargingRecordCount(chargingRecordCount);
				CostTotalRatio.setCreateUser(this.getUesr().getUserID());
				CostTotalRatio.setUpdateUser(this.getUesr().getUserID());
				costRatioList.add(CostTotalRatio);

				ChargingCostRatio CostFixedRatio = new ChargingCostRatio();
				CostFixedRatio.setBalanceMonth(balanceMonth);
				CostFixedRatio.setBalanceBillID(BalanceBillID);
				CostFixedRatio.setBalanceRatioType("1");
				CostFixedRatio.setChargingStationID("1");
				if (fixedRatio == null || fixedRatio.isEmpty()) {
					fixedRatio = "1.0";
				}
				CostFixedRatio.setBalanceRatio(Double.parseDouble(fixedRatio));
				CostFixedRatio.setTradeKWH(tradeKWHTotal);
				CostFixedRatio.setChargingKWH(chargingKWHTotal);
				CostFixedRatio.setChargingRecordCount(chargingRecordCount);
				CostFixedRatio.setCreateUser(this.getUesr().getUserID());
				CostFixedRatio.setUpdateUser(this.getUesr().getUserID());
				costRatioList.add(CostFixedRatio);

				int count = costManageService.saveChargingCostRatio(costRatioList);
				if (count > 0) {
					this.setSessionAttribute("costRatioList", null);
					map.put("result", "Success");
				} else {
					map.put("result", "NotFound");
				}
			} else 	{
				map.put("result", "NotFound");
			}
		}  catch (Exception e) {
			e.printStackTrace();
			map.put("result", "Error");
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "电费系数添加异常");
		}
		return map;
	}

	@RequestMapping(value = "/searchOilPriceManage")
	@ResponseBody
	public Map<String, Object> searchOilPriceManage(ChargingOilPrice oilPriceManage){

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "油价查询");

		// 获取所有设备数据
		List<ChargingOilPrice> list = null;
		try {
			list = costManageService.getOilPriceManage(oilPriceManage);
			for(ChargingOilPrice chargingOilPrice : list){
				String [] time = Tools.date2Str(chargingOilPrice.getOilEffectiveDTTM()).split("[^0-9]");
				String path = Const.BALANCE_PATH.concat("oilPicture").concat(File.separator).concat(time[0]).concat(File.separator).concat(time[1]).concat(File.separator).concat(time[0]).concat(time[1]).concat(time[2]).concat(".pdf");
				File file = new File(path);
				if (file.exists()) {
					chargingOilPrice.setVoucherImageLength(file.length());
				} else {
					chargingOilPrice.setVoucherImagePath(null);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "油价查询异常" + e.getMessage());
			return map;
		}

		map.put("oilPriceManageList", list);

		oilPriceManage = null;
		return map;
	}

	@RequestMapping(value = "getVoucherImg")
	@ResponseBody
	public Map<String, Object> getVoucherImg(@RequestParam(value="oilEffectiveDate", required=false) String oilEffectiveDate,
			@RequestParam(value="checkFlag", required=false) String checkFlag, HttpServletResponse response) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		ChargingOilPrice chargingOilPrice = new ChargingOilPrice();
		chargingOilPrice.setOilEffectiveDate(oilEffectiveDate);
		String fileName = costManageService.getOilPriceManage(chargingOilPrice).get(0).getVoucherImagePath();
		if(fileName != null && !fileName.isEmpty()){
			String [] oilEffectiveDateArr = oilEffectiveDate.split("[^\\d]");
			String path = Const.BALANCE_PATH.concat("oilPicture").concat(File.separator).concat(oilEffectiveDateArr[0]).concat(File.separator).concat(oilEffectiveDateArr[1]).concat(File.separator);
			path = path + fileName;
			if (checkFlag != null && !checkFlag.isEmpty()) {
				logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "油价文件下载");
				try{
					FileDownload.fileDownload(response, path, fileName);
					logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "油价文件下载:" + Const.BALANCE_PATH.concat("oilPicture").concat(File.separator).concat(oilEffectiveDateArr[0]).concat(File.separator).concat(oilEffectiveDateArr[1]).concat(File.separator));
				} catch (Exception e){
					logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "油价文件下载异常" + e.getMessage());
				}
			} else {
				File file = new File(path);
				if (file.exists()) {
					map.put("result", "Success");
				} else {
					map.put("result", "NotFound");
				}
			}
		} else {
			map.put("result", "NotFound");
		}
		return map;
	}

	@ResponseBody
	@RequestMapping(value = "/saveOilPriceManage")
	public Map<String, Object> saveOilPriceManage(HttpServletRequest request){

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "油价添加");
		if(((MultipartHttpServletRequest) request).getFile("uploadFile") == null || ((MultipartHttpServletRequest) request).getFile("uploadFile").isEmpty()){
			map.put("result", "NotFoundFile");
		} else {
			MultipartFile  file = null;
			file = ((MultipartHttpServletRequest) request).getFile("uploadFile");
			if (file != null) {
				ChargingOilPrice oilPriceManage = new ChargingOilPrice();
				String [] oilEffectiveDateArr = request.getParameter("OilEffectiveDate").split("[^\\d]");
				String path = Const.BALANCE_PATH.concat("oilPicture").concat(File.separator).concat(oilEffectiveDateArr[0]).concat(File.separator).concat(oilEffectiveDateArr[1]).concat(File.separator);
				String fileName = oilEffectiveDateArr[0].concat(oilEffectiveDateArr[1]).concat(oilEffectiveDateArr[2]).concat(".pdf");
				oilPriceManage.setVoucherImagePath(fileName);
				oilPriceManage.setOilEffectiveDTTM(Tools.str2Date(request.getParameter("OilEffectiveDate")));
				if(request.getParameter("OilPrice") == null || request.getParameter("OilPrice").isEmpty()){
					oilPriceManage.setOilPrice(0);
				} else {
					oilPriceManage.setOilPrice(Double.parseDouble(request.getParameter("OilPrice")));
				}
				if(request.getParameter("ServicePriceRate") == null || request.getParameter("ServicePriceRate").isEmpty()){
					oilPriceManage.setServicePriceRate(0);
				} else {
					oilPriceManage.setServicePriceRate(Double.parseDouble(request.getParameter("ServicePriceRate")));
				}
		        oilPriceManage.setProposer("-");
				oilPriceManage.setApprover("-");
				oilPriceManage.setCreateUser(this.getUesrInfo().getUserID());
				oilPriceManage.setUpdateUser(this.getUesrInfo().getUserID());
				try {
					int count = costManageService.saveChargingOilPrice(oilPriceManage);
					FileUpload.upload(file, path, fileName, null);
					if (count > 0) {
						map.put("result", "Success");
					} else {
						map.put("result", "NotFound");
						logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "油价添加失败");
					}
				} catch (DuplicateKeyException ex) {
					String[] error = ex.getCause().getMessage().split("'");
					if ("PRIMARY".equals(error[3].trim())) {
						map.put("result", "OilEffectiveDTTMNoNull");
					}
				} catch (Exception e) {
					e.printStackTrace();
					map.put("result", "Error");
					logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "油价添加异常:" + e.getMessage());
				}
				oilPriceManage = null;
			} else {
				map.put("result", "NotFoundFile");
			}
		}
		return map;
	}

	@RequestMapping(value = "/importOilImage")
	@ResponseBody
	public Map<String, Object> importChargingRecord(@RequestParam(value="importFile", required=false) MultipartFile file, String oilEffectiveDate) {

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "油价文件导入");
		if (file != null && !file.isEmpty()) {
			try {
				String [] oilEffectiveDateArr = oilEffectiveDate.split("[^\\d]");
				String path = Const.BALANCE_PATH.concat("oilPicture").concat(File.separator).concat(oilEffectiveDateArr[0]).concat(File.separator).concat(oilEffectiveDateArr[1]).concat(File.separator);
				String fileName = oilEffectiveDateArr[0].concat(oilEffectiveDateArr[1]).concat(oilEffectiveDateArr[2]).concat(".pdf");
				FileUpload.upload(file, path, fileName, null);
				map.put("result", "Success");
			} catch (Exception e) {
				map.put("result", "Error");
				logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "油价文件【" +  file.getOriginalFilename() + "】导入异常：" + e.getMessage());
				e.printStackTrace();
			}
		} else {
			map.put("result", "NotFound");
			logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "导入失败，导入文件不存在!");
		}
		return map;
	}

	@RequestMapping(value = "/deleteOilPriceManage")
	@ResponseBody
	public Object deleteOilPriceManage(String oilEffectiveDate) {

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "油价删除");
		ChargingOilPrice oilPriceManage = new ChargingOilPrice();
		int count = 0;
		try {
			oilPriceManage.setOilEffectiveDTTM(Tools.str2Date(oilEffectiveDate));
			deleteOilFile(oilEffectiveDate);
			count = costManageService.deleteChargingOilPrice(oilPriceManage);
			if (count > 0) {
				map.put("result", "Success");
			} else {
				map.put("result", "NotFound");
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("result", "Error");
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "油价信息删除异常:" + e.getMessage());
		}
		oilPriceManage = null;
		return map;
	}

	public void deleteOilFile(String time){
		String [] meterTimeArr = time.split("[^\\d]");
		String filePath = Const.BALANCE_PATH.concat("oilPicture").concat(File.separator)
				.concat(meterTimeArr[0]).concat(File.separator).concat(meterTimeArr[1])
				.concat(File.separator).concat(meterTimeArr[0]).concat(meterTimeArr[1]).concat(meterTimeArr[2]).concat(".pdf");
		File file = new File(filePath);
		if (file.exists()) {
			file.delete();
		}
	}

	@RequestMapping(value = "/searchChargingTimePrice")
	@ResponseBody
	public Map<String, Object> searchChargingTimePrice(ChargingTimePrice chargingTimePrice){

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "平谷电价查询");

		// 获取所有设备数据
		List<ChargingTimePrice> list = null;
		try {
			list = costManageService.getChargingTimePrice(chargingTimePrice);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "平谷电价查询异常" + e.getMessage());
			return map;
		}
		map.put("chargingTimePriceList", list);
		chargingTimePrice = null;
		return map;
	}

	@ResponseBody
	@RequestMapping(value = "/saveChargingTimePrice")
	public Map<String, Object> saveChargingTimePrice(ChargingTimePrice chargingTimePrice){

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "平谷电价添加");

		int count = 0;
		try {
			chargingTimePrice.setVoucherImagePathOne("-");
			chargingTimePrice.setVoucherImagePathTwo("-");
			chargingTimePrice.setCreateUser(this.getUesrInfo().getUserID());
			chargingTimePrice.setUpdateUser(this.getUesrInfo().getUserID());
			count = costManageService.saveChargingTimePrice(chargingTimePrice);
			if (count > 0) {
				this.setSessionAttribute(Const.SESSION_TIMEPRICE, null);
				map.put("result", "Success");
			} else {
				map.put("result", "NotFound");
			}
		} catch (DuplicateKeyException ex) {
			String[] error = ex.getCause().getMessage().split("'");
			if ("PRIMARY".equals(error[3].trim())) {
				map.put("result", "PriceTypeExist");
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("result", "Error");
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "平谷电价添加异常");
		}
		chargingTimePrice = null;
		return map;
	}

	@ResponseBody
	@RequestMapping(value = "/updateChargingTimePrice")
	public Map<String, Object> updateChargingTimePrice(ChargingTimePrice chargingTimePrice){

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "平谷电价修改");
		int count = 0;
		chargingTimePrice.setUpdateUser(this.getUesrInfo().getUserID());
		try {
			count = costManageService.updateChargingTimePrice(chargingTimePrice);
			if (count > 0) {
				map.put("result", "Success");
				this.setSessionAttribute(Const.SESSION_TIMEPRICE, null);
			} else {
				map.put("result", "NotFound");
			}
		}  catch (Exception e) {
			e.printStackTrace();
			map.put("result", "Error");
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "平谷电价修改异常");
		}
		chargingTimePrice = null;
		return map;
	}

	@RequestMapping(value = "/deleteChargingTimePrice")
	@ResponseBody
	public Object deleteChargingTimePrice(ChargingTimePrice chargingTimePrice) {

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "平谷电价删除");
		int count = 0;
		try {
			count = costManageService.deleteChargingTimePrice(chargingTimePrice);
			if (count > 0) {
				this.setSessionAttribute(Const.SESSION_TIMEPRICE, null);
				map.put("result", "Success");
			} else {
				map.put("result", "NotFound");
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("result", "Error");
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "平谷电价信除异常:" + e.getMessage());
		}
		chargingTimePrice = null;
		return map;
	}

	@RequestMapping(value = "/searchChargingFixedPrice")
	@ResponseBody
	public Map<String, Object> searchChargingFixedPrice(ChargingFixedPrice chargingFixedPrice){

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "定电价查询");

		// 获取所有设备数据
		List<ChargingFixedPrice> list = null;
		try {
			list = costManageService.getChargingFixedPrice(chargingFixedPrice);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "定电价查询异常" + e.getMessage());
			return map;
		}

		map.put("chargingFixedPriceList", list);

		chargingFixedPrice = null;
		return map;
	}

	@ResponseBody
	@RequestMapping(value = "/saveChargingFixedPrice")
	public Map<String, Object> saveChargingFixedPrice(ChargingFixedPrice chargingFixedPrice){

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "定电价添加");

		int count = 0;
		try {
			chargingFixedPrice.setEffectiveDTTM(Tools.str2Date(chargingFixedPrice.getEffectiveDate()));
			chargingFixedPrice.setVoucherImagePathOne("-");
			chargingFixedPrice.setVoucherImagePathTwo("-");
			chargingFixedPrice.setCreateUser(this.getUesrInfo().getUserID());
			chargingFixedPrice.setUpdateUser(this.getUesrInfo().getUserID());
			count = costManageService.saveChargingFixedPrice(chargingFixedPrice);
			if (count > 0) {
				this.setSessionAttribute(Const.SESSION_FIXEDPRICE, null);
				map.put("result", "Success");
			} else {
				map.put("result", "NotFound");
			}
		}  catch (Exception e) {
			e.printStackTrace();
			map.put("result", "Error");
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "定电价添加异常");
		}
		chargingFixedPrice = null;
		return map;
	}

	@ResponseBody
	@RequestMapping(value = "/updateChargingFixedPrice")
	public Map<String, Object> updateChargingFixedPrice(String effectiveDate){

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "默认定电价修改");
		BalanceInitSetting balanceInitSetting = new BalanceInitSetting();
		int count = 0;
		try {
			balanceInitSetting.setUpdateUser(this.getUesrInfo().getUserID());
			balanceInitSetting.setDefaultFixedPriceDate(Tools.str2Date(effectiveDate));
			count = costManageService.updateChargingFixedPrice(balanceInitSetting);
			if (count > 0) {
				this.setSessionAttribute(Const.SESSION_FIXEDPRICE, null);
				map.put("result", "Success");
			} else {
				map.put("result", "NotFound");
			}
		}  catch (Exception e) {
			e.printStackTrace();
			map.put("result", "Error");
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "默认定电价修改异常");
		}
		balanceInitSetting = null;
		return map;
	}

	@RequestMapping(value = "/deleteChargingFixedPrice")
	@ResponseBody
	public Object deleteChargingFixedPrice(String effectiveDate) {

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "定电价删除");
		ChargingFixedPrice chargingFixedPrice = new ChargingFixedPrice();
		int count = 0;
		try {
			chargingFixedPrice.setEffectiveDTTM(Tools.str2Date(effectiveDate));
			count = costManageService.deleteChargingFixedPrice(chargingFixedPrice);
			if (count > 0) {
				this.setSessionAttribute(Const.SESSION_FIXEDPRICE, null);
				map.put("result", "Success");
			} else {
				map.put("result", "NotFound");
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("result", "Error");
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "定电价删除异常:" + e.getMessage());
		}
		chargingFixedPrice = null;
		return map;
	}

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/searchBalanceInitSetting")
	@ResponseBody
	public Map<String, Object> searchBalanceInitSetting(BalanceInitSetting balanceInitSetting){

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "电价设置查询");
		// 获取所有设备数据
		List<BalanceInitSetting> list = null;
		try {
			list = (List<BalanceInitSetting>)this.getSessionAttribute("BalanceInitSetting");
			if (list == null) {
				list = costManageService.getBalanceInitSetting(balanceInitSetting);
				this.setSessionAttribute("BalanceInitSetting", list);
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "电价设置查询异常" + e.getMessage());
			return map;
		}

		map.put("balanceInitSettingList", list);

		balanceInitSetting = null;
		return map;
	}

	@ResponseBody
	@RequestMapping(value = "/updateBalanceInitSetting")
	public Map<String, Object> updateBalanceInitSetting(BalanceInitSetting balanceInitSetting){

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "电价设置修改");

		int count = 0;
		try {
			balanceInitSetting.setCreateUser(this.getUesrInfo().getUserID());
			balanceInitSetting.setUpdateUser(this.getUesrInfo().getUserID());
			count = costManageService.saveBalanceInitSetting(balanceInitSetting);
			if (count > 0) {
				map.put("result", "Success");
				this.setSessionAttribute("BalanceInitSetting", null);
			} else {
				map.put("result", "NotFound");
			}
		}  catch (Exception e) {
			e.printStackTrace();
			map.put("result", "Error");
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "电价设置修改异常");
		}
		balanceInitSetting = null;
		return map;
	}

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/chargingFixedPriceName")
	@ResponseBody
	public List<ChargingFixedPrice> getChargingFixedPriceName(ChargingFixedPrice chargingFixedPrice){

		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "下拉列表-定电价查询");
		List<ChargingFixedPrice> list = null;
		try {
			list = (List<ChargingFixedPrice>)this.getSessionAttribute(Const.SESSION_FIXEDPRICE);
			if (list == null) {
				list = costManageService.getChargingFixedPriceName(chargingFixedPrice);
				this.setSessionAttribute(Const.SESSION_FIXEDPRICE, list);
			}
		} catch (Exception e) {
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "下拉列表-定电价查询异常:" + e.getMessage());
			return null;
		}
		return list;
	}

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/chargingTimePriceName")
	@ResponseBody
	public List<ChargingTimePrice> getChargingTimePriceName(ChargingTimePrice chargingTimePrice){

		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "下拉列表-平谷电价查询");
		List<ChargingTimePrice> list = null;
		try {
			list = (List<ChargingTimePrice>)this.getSessionAttribute(Const.SESSION_TIMEPRICE);
			if (list == null) {
				list = costManageService.getChargingTimePriceName(chargingTimePrice);
				this.setSessionAttribute(Const.SESSION_TIMEPRICE, list);
			}

		} catch (Exception e) {
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "下拉列表-平谷电价查询异常:" + e.getMessage());
			return null;
		}
		return list;
	}

	@RequestMapping(value = "/resetAuthority")
	@ResponseBody
	public Map<String, Object> resetAuthority() {

		Map<String, Object> map = new HashMap<String, Object>();
		mySecurityMetadataSource.reloadResourceDefine();
		map.put("result", "Success");
		return map;
	}
	
}
